package com.cuixichang.minimalism.core.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseConfigParam;
import com.cuixichang.minimalism.core.mybatis.utils.CodeGeneratorUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@MapperScan(BaseConfigParam.MAPPER_SCAN_PATH)
public class MybatisPlusConfig {
    @Autowired
    private DataSource dataSource;
    @Bean
    public CodeGeneratorUtil codeGeneratorUtil(){
        return new CodeGeneratorUtil(new DataSourceConfig.Builder(dataSource));
    }
    @Bean
    public MybatisPlusInterceptor paginationInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}
